package com.study.chapter05.operator.transform;

import com.study.entity.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/7/28 10:26
 */
public class MapOperatorTest {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.fromElements(
                new Event("Bob", "./home", 10000L),
                new Event("Alice", "./pred?id=20", 10000L)
        );

        //1.使用lambda表达式
        SingleOutputStreamOperator<String> result = stream.map(Event::getUser).returns(Types.STRING);

        //2.使用匿名类实现MapFunction
        SingleOutputStreamOperator<String> result2 = stream.map(new MapFunction<Event, String>() {
            @Override
            public String map(Event event) throws Exception {
                return event.getUser();
            }
        });

        //3.使用MapFunction的实现类
        SingleOutputStreamOperator<String> result3 = stream.map(new MyMapFunction());


        result.print("lambda");
        result2.print("匿名：");
        result3.print("实现类：");

        env.execute();
    }

    public static class MyMapFunction implements MapFunction<Event,String> {

        @Override
        public String map(Event event) throws Exception {
            return event.getUser();
        }
    }
}
